<?php


namespace App\common\components;


use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;

/**
 * @desc controller基类
 * Class BaseController
 * @package App\common\components
 */
class BaseController extends Controller
{
    use DispatchesJobs, ValidatesRequests;

    /**
     * 用户端模拟数据地址
     */
    const UserFalseDataDir = '\falseData';
    /**
     * 商户端模拟数据地址
     */
    const StoreFalseDataDir = '\storeFalseData';

    /**
     * 全局获取数据
     * @param $class 模型名称
     * @param $action 方法名称
     * @param $code  是否真实数据，默认假数据
     * @param $type  小程序端口 1用户 2商家
     * @return array|mixed
     */
    public static function getData($class, $action, $code = 0,$type = 0)
    {
        $dataTrueFalse = config('config.dataTrueFalse');
        $lists = [];
        //获取数据
        if ($dataTrueFalse != 'false' || $code > 0) {
            return $lists;
        }
        $useClassDir = '\App\common\models';
        switch ($type) {
            case  1://用户端
                $useClassDir .= self::UserFalseDataDir;
                break;
            case 2://商户端
                $useClassDir .= self::StoreFalseDataDir;
                break;
            default :
                $useClassDir .= self::UserFalseDataDir;
                break;
        }
        $useClassDir = $useClassDir . "\\" . $class;
        $lists = $useClassDir::$action();

        return $lists;

    }

    public function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json'
            ));
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);

        return $output;
        exit();

    }

}
